<?php
namespace App\Repositories;

use App\Models\Video;
use App\Models\VideoLearn;

class VideoRepository extends BaseRepository
{

    protected $service;
    public function __construct(
        Video $video,
        ServiceRepository $serviceRepository
    )
    {
        $this->model = $video;
        $this->service = $serviceRepository;
    }


    /**
     * 获取理论学习列表
     * @param array $data
     * @param array|string $extra
     * @param string $size
     * @return string
     */
    public function index($data, $student_id, $size = '')
    {

        $theory_list = $this->model->where($data)->whereIn('parentid', [281,283])->get()->toArray();

        $theory_mode[] = [
            "id"=>"281",
            "name"=>"科目一",
            "parentid"=>"280",
            "big"=>"0",
            "url"=>"",
            "desc"=>"",
            "typeid"=>"1",
            "status"=>"1",
            "listorder"=>"1",
        ];


        $theory_mode[] = [
            "id"=> "283",
            "name"=> "科目四",
            "parentid"=> "280",
            "big"=> "0",
            "url"=> "",
            "desc"=> "",
            "typeid"=> "1",
            "status"=> "1",
            "listorder"=> "1",
        ];


        $result = [];
        if($theory_list){

            foreach($theory_mode as $mode){

                $sum = 0;
                $learn = 0;

                foreach($theory_list as $theory){

                    $theory['studentstatus'] = 0;

                    $theory['child'] = [];

                    if($theory['parentid'] == $mode['id']){

                        if(VideoLearn::where([['video_id', $theory['id']], ['student_id', $student_id]])->first()){
                            $theory['studentstatus'] = 1;
                            $learn++;
                        }
                        $mode['child'][] = $theory;

                        $sum++;

                    }

                }

                $result['list'][] = $mode;

                $count['id'] = $mode['id'];
                $count['sum'] = $sum;
                $count['accomplish'] = $learn;

                $result['count'][] = $count;
            }

        }

        return $result;
    }


    /**
     * 存储odb
     * @param $inputs
     */
    public function store($inputs, $extra = '')
    {

    }


    /**
     * 更新
     * @param $id
     * @param $input
     * @return mixed
     */
    public function update($id, $input,$extra = '')
    {
        $model = $this->getById($id);
        return $this->save($model,$input);
    }


    /**
     * 编辑特定id资源
     * @param  int $id 资源id
     * @param  string|array $extra 可选额外传入的参数
     * @return Illuminate\Support\Collection
     */
    public function edit($id, $extra)
    {

    }

    /**
     * @param $model
     * @param $inputs
     * @param bool $falg
     * @return mixed
     */
    public function save($model , $inputs , $falg = false)
    {
        return $model;
    }


    /**
     * 检查当前用户学习视频权限
     */
    public function theoryCheck($student, $km, $video_id)
    {
        $service_info = $this->service->getByServiveInfo($student->service_id);//服务项目及权限

        if(!$service_info){
            return ['status' => false, 'msg' => '没有设置服务项目'];
        }

        //是否能学习视频
        if($service_info->config && $service_info->config->stu_video){

            //是否按照顺序学习
            if($service_info->config->stu_video_order){
                //查看当前用户的科目学习进度
                $learn_info = VideoLearn::where(['student_id' => $student->id, 'km' => $km])->orderBy('created_at', 'desc')->first();

                if($learn_info){
                    //是否紧挨着上一个视频记录ID
                    if($video_id - $learn_info->video_id > 1){
                        return ['status' => false, 'msg' => '需要按照顺序学习视频'];
                    }
                }else{

                    if($video_id != 284 && $km == 1){
                        return ['status' => false, 'msg' => '需要按照顺序学习视频'];
                    }elseif($video_id != 298 && $km == 4){
                        return ['status' => false, 'msg' => '需要按照顺序学习视频'];
                    }
                }

            }

            //是否限制时间
            if($service_info->config->stu_video_h){

                //查看当前用户的今天科目学习总时
                $learn_count = VideoLearn::where(['student_id' => $student->id])->where('created_at', '>=', date('Y-m-d'))->orderBy('created_at', 'desc')->sum('duration');
                $learn_count = $learn_count / (60 * 45);
                if($learn_count >= $service_info->config->stu_video_h){
                    return ['status' => false, 'msg' => '今天理论学习时长已经超过限制了'];
                }
            }

            return ['status' => true, 'msg' => ''];

        }else{
            return ['status' => false, 'msg' => '没有权限学习视频'];
        }
    }

}